﻿using ReflectionStudio.Core.Events;
using System;
using System.ComponentModel;
using System.Data;
using System.Windows;
using WPFExcelReport.Helpers;
using WPFExcelReport;
namespace WPFExcelReport
{
    public partial class TICC_SPINSPEED : ViewBase
    {
        public VICC_SPINSPEED TMV
        {
            get
            {
                return (VICC_SPINSPEED)MV;
            }
        }
        public TICC_SPINSPEED()
        {
            if (DesignerProperties.GetIsInDesignMode(this))
                return;
            MV = new VICC_SPINSPEED(); 
            DataContext = MV;
            InitializeComponent(); 
        }
        
        public void Load()
        {
            dwData = TBMain.NewRow(); 
            SResult = ((VICC_SPINSPEED)MV).QueryICC_SPINSPEED(dwData, 9999);
            MergeData(SResult, true, true);
        }
        
        public override void New()
        {
            base.New();
             dwData = TBMain.NewRow(); 
             ((VICC_SPINSPEED)MV).SetUpNewRow(ref dwData);
             TBMain.Rows.InsertAt(dwData, 0);
             ((VICC_SPINSPEED)MV).InsertICC_SPINSPEED(dwData, app.company);
        }
        public override void NewCommand(object sender, RoutedEventArgs e)
        {
            New();
            base.NewCommand(sender, e);
        }
        private void Load_Click(object sender, RoutedEventArgs e)
        { 
            try
            {
                Load();
            }
            catch (Exception ex)
            {
                Tracer.Error(ex.StackTrace, ex);
            }
        }

        public override void SaveCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;
            TMV.DSSPEED02(null);
            dwData = TBMain.Clone().NewRow();
            TMV.SetUpNewRow(ref dwData);
            foreach (DataRow row in TBMain.Rows)
            {
                dwData.ItemArray = row.ItemArray;
                TMV.InsertICC_SPINSPEED(dwData, app.company);
            }
            //TMV.UpdateICC_SRULES(dwMaster, 0);
            base.SaveCommand(sender, e);
        }
        public override void CopyCommand(object sender, RoutedEventArgs e)
        {
            if (dwMaster == null)
                return;

            dwData = TBMain.NewRow();
            dwData.ItemArray = dwMaster.ItemArray;
            dwData["RULEID"] = VSDDAT.SD22.GETNEXT("0022", "SR", "  ", 1, 4);
            TBMain.Rows.InsertAt(dwData, 0);
            TMV.InsertICC_SPINSPEED(dwData, app.company);
            base.CopyCommand(sender, e);
        }

        private void LoadExcelCommand(object sender, RoutedEventArgs e)
        {
            cDR.PRINT(null);
            if (cDR.SetExchange == null)
                return;
            if (cDR.SetExchange.Tables.Count == 0)
                return;
            foreach (DataRow row in cDR.SetExchange.Tables[0].Rows)
            {
                foreach (DataColumn c in cDR.SetExchange.Tables[0].Columns)
                {
                    row[c] = Helper.ConvertGB2312To8859P1(row[c].ToString());
                }
            }
            MergeData(cDR.SetExchange, true, true);
        }
    }
}